define(['jquery','vue','common'],function ($,Vue,common) {
    return {
        index:function (){
            const {createApp} = Vue
            let app = createApp({
                data() {
                    return {
                        step:0,
                        stepSuccess:false,
                        stepList:[],
                        databases:{
                            type:"mysql",
                            hostname:'127.0.0.1',
                            hostport:'3306',
                            username:'root',
                            password:'',
                            database:'',
                            charset:'utf8mb4',
                            prefix:"",
                            break_reconnect:false
                        },
                        rules:{
                            type:[
                                {required:true,message:'请选择数据库类型',trigger:'change'}
                            ],
                            hostname:[
                                {required:true,message:"请输入数据库地址",trigger:"blur"}
                            ],
                            hostport:[
                                {required:true,message:"请输入数据库端口",trigger:"blur"}
                            ],
                            database:[
                                {required:true,message:"请输入数据库名称",trigger:"blur"}
                            ],
                            username:[
                                {required:true,message:"请输入数据库用户名",trigger:"blur"}
                            ],
                            password:[
                                {required:true,message:"请输入数据库密码",trigger:"blur"}
                            ]
                        },
                        data_messgae:"",
                        dataTypeList:[
                            {
                                name:'MySQL',
                                value:'mysql'
                            },
                            {
                                name:'SQLite',
                                value:'sqlite'
                            },
                            // {
                            //     name:'PostgreSQL',
                            //     value:'pgsql'
                            // },
                            // {
                            //     name:'SQL Server',
                            //     value:'sqlsrv'
                            // },
                            // {
                            //     name:'Oracle',
                            //     value:'oracle'
                            // },
                            // {
                            //     name:'MongoDB',
                            //     value:'mongodb'
                            // }
                        ],
                        config_new:{
                            username:"admin",
                            password:"",
                            site_name:"沐雨后台"
                        },
                        config_rules:{
                            username:[
                                {
                                    required:true,
                                    message:"请输入用户名",
                                    trigger:"blur"
                                },
                                {
                                    min:4,
                                    max:20,
                                    message:"请输入6-20位密码",
                                    trigger:"blur"
                                }
                            ],
                            password:[
                                {
                                    required:true,
                                    message:"请输入密码",
                                    trigger:"blur"
                                },
                                {
                                    max:20,
                                    message:"请输入6-20位密码",
                                    trigger:"blur"
                                }
                            ],
                            site_name:[
                                {
                                    required:true,
                                    message:"请输入网站名称",
                                    trigger:"blur"
                                },
                                {
                                    min:2,
                                    max:10,
                                    message:"长度在 2 到 10 个字符",
                                    trigger:"blur"
                                }
                            ]
                        }
                    }
                },
                created(){
                    this.checkKz()
                },
                methods:{
                    gotoAdmin(){
                      window.location.href = '/app/muadmin/admin/'
                    },
                    changeDatas(){
                        let data = common.copy(this.config_new);
                        let that = this
                        common.post("/app/muadmin/admin/install/update_admin",data).then(res=>{
                            if(res.code==1){
                                common.message(res.msg,"success",()=>{
                                    that.stepSuccess = true;
                                })
                            }else{
                                common.message(res.msg,'error')
                            }
                        })
                    },
                    configRules(){
                        this.$refs.myrules.validate((valid)=>{
                            if(valid){
                                this.step = 3
                            }else{
                                this.step = 2;
                            }
                        })
                    },
                    addData(){
                        let dataConfig = JSON.parse(JSON.stringify(this.databases))
                        common.post('/app/muadmin/admin/install/install_sql',{row:dataConfig},true).then(res=>{
                            if(res.code==1){
                                common.message(res.msg)
                                this.stepSuccess = false;
                            }else{
                                common.message(res.msg,'error')
                            }
                        })
                    },
                    linkData(){
                        // console.log(this.databases)
                        let dataConfig = JSON.parse(JSON.stringify(this.databases))
                        common.post('/app/muadmin/admin/install/link_data',dataConfig).then(res=>{
                            if(res.code==1){
                                this.step  = 2
                            }else{
                                this.data_messgae = res.msg
                                common.message(res.msg,'error')
                            }
                        })
                    },
                    cxChank(){
                      this.checkKz()
                    },
                    checkKz(){
                        let that = this;
                        common.post('/app/muadmin/admin/install/check').then(res=>{
                            console.log(res)
                            if(res.code == 1){
                                let newstep = true
                                for(let i in res.data){
                                    if(res.data[i].status == 'error'){
                                        newstep = false
                                        break
                                    }
                                }
                                if(newstep){
                                    that.step = 1
                                }
                                that.stepList = res.data
                            }
                        })
                    }
                }
            })

            common.load(app,['icon'])
            app.mount('#app')
            // console.log(11111)
        }
    }
})